<!Doctype html>
<html>

<head>
    <title>oixan的博客</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html" ; charset="utf-8">
    <link rel="stylesheet" type="text/css" href="../css/blog.css">
    <link rel="stylesheet" type="text/css" href="./bcss/swiper.min.css">
</head>

<body>
    <div class="head">
        <div class="headnav">
            <div class="naveframe">
                <ul class="navlist">
                    <li>
                        <a href="../index.html">
                            主页
                        </a>
                    </li>
                    <li>
                        <a href="../bloglist.html">
                            博客
                        </a>
                    </li>
                    <li>
                        <a href="../about.html">
                            关于
                        </a>
                    </li>
                </ul>
            </div>
        </div>

        <div class="headtext">
            <div class="headtext1">世界很简单，人类很复杂。</div>
            <div class="headtext2">浮生若梦，寻寻觅觅。</div>
        </div>
    </div>

    <div class="contents-all">
        <div class="contents">

            <div class="frame">
                <div class="title">
                    <h3>python加密解密AES</h3>
                </div>
                <div class="dtime">时间：2018-07-16</div>
                <div class="detail">


                        <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-size: 12px;">from Crypto.Cipher import AES</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">from binascii import b2a_hex,a2b_hex&nbsp;&nbsp;</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">import base64</span></strong></span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">def the_str16(st): #补16位</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; st=zwencode(st)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; while len(st)%16!=0:</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; st=st+&#39;\0&#39;</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; return str.encode(st)</span></strong></span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">def zwencode(st): #编码</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; st=base64.b64encode(st.encode(&#39;utf-8&#39;))</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; st=st.decode()</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; return st</span></strong></span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">def zwdecode(st): #解码</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; missing_padding = 4 - len(st) % 4</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; if missing_padding:</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; st += &#39;=&#39;* missing_padding</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp;&nbsp;</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; st=base64.b64decode(st)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; st=st.decode(&#39;utf-8&#39;)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; return st</span></strong></span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">def encryt_ecb(key,st): #加密</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; st=the_str16(st)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; key=the_str16(key)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; aes=AES.new(key,AES.MODE_ECB)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; text_str=aes.encrypt(st)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; return b2a_hex(text_str)</span></strong></span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">def decryt_ecb(key,st): #解密</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; key=the_str16(key)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; aes=AES.new(key,AES.MODE_ECB)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; text_str=aes.decrypt(a2b_hex(st))</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; return text_str</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">if __name__==&#39;__main__&#39;:</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; isjiami=input(&#39;输入encode加密，decode解密：&#39;)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; if isjiami == &#39;encode&#39;:</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; text=input(&#39;输入要加密字符：&#39;)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; key=input(&#39;输入key:&#39;)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; try:</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(encryt_ecb(key,text).decode())</span></strong></span>
                            </p>
                           
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; except:</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;加密失败&#39;)&nbsp; &nbsp; &nbsp; &nbsp;</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; if isjiami == &#39;decode&#39;:</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; text=input(&#39;输入要解密字符：&#39;)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; key=input(&#39;输入key:&#39;)</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; try:</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(zwdecode(decryt_ecb(key,text).decode()))</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; except:</span></strong></span>
                            </p>
                            <p>
                                <span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; font-size: 12px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;解密失败&#39;)</span></strong></span>
                            </p>
                            <p>
                                <br/>
                            </p>


                </div>
                <a href="javascript:void(0)" onclick="showPg()">返回</a>
            </div>

        </div>
    </div>
    <div class="footer">
        <div class="footertext">©2018,oixan博客</div>
    </div>
    <span id="backtop" onclick="topFunction()">返回顶部</span>
    
</body>
<script  src="../js/fanhuiye.js"></script>

</html>